using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

namespace UndergroundSurvival.Rooms
{
    public enum RoomType
    {
        Living,
        Storage,
        Farm,
        Entertainment,
        Power,
        Security,
        Engineering
    }

    public class Room : MonoBehaviour
    {
        [Header("Room Properties")]
        public RoomType type;
        public string roomName;
        public string description;
        public Sprite icon;
        public int level = 1;
        public int maxLevel = 3;
        public float health = 100f;
        public float maxHealth = 100f;
        public float efficiency = 1f;

        [Header("Resource Requirements")]
        public List<ResourceCost> buildCosts;
        public List<ResourceCost> maintenanceCosts;

        [Header("Room Stats")]
        public float powerConsumption;
        public float waterConsumption;
        public float airConsumption;
        public float temperature;

        [Header("Room State")]
        public bool isBuilt = false;
        public bool isFunctioning = false;

        [Header("Visual Components")]
        [SerializeField] private SpriteRenderer roomSprite;
        [SerializeField] private SpriteRenderer previewSprite;
        [SerializeField] private BoxCollider2D roomCollider;

        private void Awake()
        {
            if (roomSprite == null)
                roomSprite = GetComponent<SpriteRenderer>();
            if (roomCollider == null)
                roomCollider = GetComponent<BoxCollider2D>();
        }

        public virtual void Build()
        {
            isBuilt = true;
            isFunctioning = true;
            if (roomCollider != null)
                roomCollider.enabled = true;
            if (roomSprite != null)
                roomSprite.enabled = true;
            if (previewSprite != null)
                previewSprite.enabled = false;

            // 注册到资源传输系统
            if (ResourceTransportManager.Instance != null)
                ResourceTransportManager.Instance.RegisterRoom(this);
        }

        public virtual void Demolish()
        {
            // 注销资源传输系统
            if (ResourceTransportManager.Instance != null)
                ResourceTransportManager.Instance.UnregisterRoom(this);

            isBuilt = false;
            isFunctioning = false;
        }

        public virtual void Upgrade()
        {
            if (level < maxLevel)
            {
                level++;
                maxHealth *= 1.5f;
                health = maxHealth;
                efficiency *= 1.2f;
            }
        }

        public virtual void Repair()
        {
            health = maxHealth;
            isFunctioning = true;
        }

        public virtual void OnRoomDamaged(float damage)
        {
            health = Mathf.Max(0, health - damage);
            if (health <= 0)
            {
                Demolish();
            }
            else if (health < maxHealth * 0.3f)
            {
                isFunctioning = false;
            }
        }

        public void SetPreviewMode(bool isPreview)
        {
            if (roomSprite != null)
                roomSprite.enabled = !isPreview;
            if (previewSprite != null)
                previewSprite.enabled = isPreview;
            if (roomCollider != null)
                roomCollider.enabled = !isPreview;
        }

        public void SetPreviewValid(bool isValid)
        {
            if (previewSprite != null)
            {
                previewSprite.color = isValid ? Color.green : Color.red;
            }
        }

        private void OnMouseDown()
        {
            if (isBuilt && BuildingManager.Instance != null)
            {
                BuildingManager.Instance.ShowRoomUI(this);
            }
        }
    }

    [System.Serializable]
    public class ResourceCost
    {
        public string resourceName;
        public float amount;
    }
} 